package com.vtc.session4;

import com.vtc.R;
import com.vtc.session3.Employee;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class AddActivity extends Activity implements OnClickListener{

	private EditText txtFullName;
	private RadioGroup rdGroup;
	private EditText txtAge;
	private Button btnSave;
	
	private Employee emp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.add1_activity);
		this.txtFullName = (EditText) findViewById(R.id.add_txtFullName);
		this.txtAge = (EditText) findViewById(R.id.add_txtAge);
		this.rdGroup= (RadioGroup) findViewById(R.id.rdGender);
		this.btnSave= (Button) findViewById(R.id.add_btnSave);
		emp =(Employee) getIntent().getSerializableExtra("xxx");
		if(emp!=null){
			txtFullName.setText(emp.getName());
			txtAge.setText(emp.getAge()+"");
			if(emp.getGender().equals("Male")){
				RadioButton btn1= (RadioButton) findViewById(R.id.add_rdMale);
				btn1.setChecked(true);
			}
			else{
				RadioButton btn1= (RadioButton) findViewById(R.id.add_rdFemale);
				btn1.setChecked(true);
			}
		}		
		// registry listener
		btnSave.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==btnSave){
			String fullName = txtFullName.getText().toString();
			int age = Integer.parseInt(txtAge.getText().toString());
			String gender;
			
			
			if(rdGroup.getCheckedRadioButtonId()== R.id.add_rdMale){
				gender = "Male";
			}
			else{
				gender= "Female";
			}
			Employee e = new Employee(fullName,gender,age);
			if(emp==null){
			    ListView2Actvity.adapter.add(e);
			}
			else{
				e.setImageId(emp.getImageId());
				ListView2Actvity.adapter.update(e);
			}
			Intent i = new Intent(this,ListView2Actvity.class);
			startActivity(i);
		}
	}
}
